package com.huawei123.hasee.cniao5store.mvp.view.fragment;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.format.Formatter;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.huawei123.hasee.cniao5store.R;
import com.huawei123.hasee.cniao5store.base.BaseMvpFragment;
import com.huawei123.hasee.cniao5store.bean.AppIntroductionBean;
import com.huawei123.hasee.cniao5store.mvp.presenter.impl.AppIntroductionFragmentPresenterImpl;
import com.huawei123.hasee.cniao5store.mvp.view.acyivity.AppDetailActivity;
import com.huawei123.hasee.cniao5store.mvp.view.acyivity.GalleryActivity;
import com.huawei123.hasee.cniao5store.mvp.view.view.AppIntroductionFragmentView;
import com.huawei123.hasee.cniao5store.utils.UIUtils;
import com.huawei123.hasee.cniao5store.view.LoadingPager;
import com.huawei123.hasee.cniao5store.view.widget.FlowLayout;
import com.huawei123.hasee.cniao5store.view.widget.FoldingTextView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目名称：Cniao5Store
 * 类描述：下载应用详情中的介绍Fragment
 * 创建人：luohao
 * 创建时间：2018/2/9 18:22
 * 修改人：luohao
 * 修改时间：2018/2/9 18:22
 * 修改备注：
 */

public class AppIntroductionFragment extends BaseMvpFragment<AppIntroductionFragmentPresenterImpl> implements
        AppIntroductionFragmentView, View.OnClickListener {

    @BindView(R.id.app_detail_gallery_container_linearlayout)
    LinearLayout app_detail_gallery_container;
    @BindView(R.id.detail_appinfo_tariff_textview)
    TextView appInfoTariff;
    @BindView(R.id.detail_appinfo_size_textview)
    TextView appInfoSize;
    @BindView(R.id.detail_appinfo_version_textview)
    TextView appInfoVersion;
    @BindView(R.id.detail_appinfo_release_date_textview)
    TextView appInfoDate;
    @BindView(R.id.appdetail_appinfo_developer_textview)
    TextView appInfoDeveloper;
    @BindView(R.id.ll)
    LinearLayout appInfoDes;
    @BindView(R.id.flowLayout)
    FlowLayout flowLayout;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        show();
    }

    @Inject
    public AppIntroductionFragmentPresenterImpl appIntroductionFragmentPresenter;
    //数据对象
    private AppIntroductionBean mAppIntroductionBean;

    /**
     * 注解绑定
     *
     * @return
     */
    @Override
    protected AppIntroductionFragmentPresenterImpl initPresenterInjector() {
        fragmentComponent.inject(this);
        return appIntroductionFragmentPresenter;
    }

    /**
     * 请求应用详情数据
     */
    @Override
    protected void loading() {
        appIntroductionFragmentPresenter.getAppInroductionData(myActivity, ((AppDetailActivity) getActivity())
                .getAppPackageName());
    }

    /**
     * 数据请求成功
     *
     * @param appIntroductionBean
     */
    @Override
    public void onAppIntroductionDataSuccess(AppIntroductionBean appIntroductionBean) {
        this.mAppIntroductionBean = appIntroductionBean;
        setState(LoadingPager.LoadResult.success);
    }

    /**
     * 数据请求失败
     *
     * @param msg
     */
    @Override
    public void onAppIntroductionDataError(String msg) {
        setState(LoadingPager.LoadResult.error);
    }

    /**
     * 数据请求成功后创建视图控件并填值
     *
     * @return
     */
    @TargetApi(Build.VERSION_CODES.DONUT)
    @Override
    protected View createSuccessView() {
        View view = UIUtils.inflate(R.layout.fragment_app_introduction);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    /**
     * 各控件填值
     */
    @TargetApi(Build.VERSION_CODES.DONUT)
    public void initView() {
        for (int i = 0; i < mAppIntroductionBean.getImageCompressList().size(); i++) {
            String url = mAppIntroductionBean.getImageCompressList().get(i);
            View screenView = View.inflate(getContext(), R.layout.appdetail_item_screen_image, null);
            ImageView screenImageView = (ImageView) screenView.findViewById(R.id.appdetail_screen_img_imageview);
            screenImageView.setContentDescription(screenImageView.getResources().getString(R.string.appdetail_screenshot));
            screenImageView.setScaleType(ImageView.ScaleType.FIT_XY);
            screenView.setOnClickListener(this);
            screenView.setTag(i);
            Glide.with(UIUtils.getContext()).load(url).into(screenImageView);
            app_detail_gallery_container.addView(screenView);
        }

        appInfoTariff.setText(mAppIntroductionBean.getAppInfoBean().getTariffDesc());
        appInfoSize.setText(Formatter.formatFileSize(getContext(), Long.parseLong(mAppIntroductionBean.getAppInfoBean().getSize
                ())));
        appInfoDate.setText(mAppIntroductionBean.getAppInfoBean().getReleaseDate());
        appInfoVersion.setText(mAppIntroductionBean.getAppInfoBean().getVersion());
        appInfoDeveloper.setText(mAppIntroductionBean.getAppInfoBean().getDeveloper());

        for (int i = 0; i < mAppIntroductionBean.getAppDetailInfoBeanList().size(); i++) {
            FoldingTextView foldingTextView = new FoldingTextView(getContext());
            foldingTextView.setTitle(mAppIntroductionBean.getAppDetailInfoBeanList().get(i).getTitle());
            foldingTextView.setContent(mAppIntroductionBean.getAppDetailInfoBeanList().get(i).getBody());
            appInfoDes.addView(foldingTextView);
        }

        List<String> tagList = mAppIntroductionBean.getTagList();
        for (int i = 0; i < tagList.size(); i++) {
            View labView = UIUtils.inflate(R.layout.appdetail_item_label_item);
            TextView tv = (TextView) labView.findViewById(R.id.appdetail_label_content_textview);
            tv.setText(tagList.get(i));
            flowLayout.addView(labView);
        }
    }

    /**
     * 在点击App详情页中介绍图片跳转图片展示Activity
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int tag = (int) v.getTag();
        List<String> images = mAppIntroductionBean.getImagesList();
        Intent intent = new Intent(getContext(), GalleryActivity.class);
        intent.putExtra("tag", tag);
        intent.putStringArrayListExtra("urlList", (ArrayList<String>) images);
        getActivity().startActivity(intent);
    }


}
